<?php

define('IN_T',true);
require_once "./source/include/init.php";

$act = Common::sfilter($_REQUEST['act']);
$input = $Json->decode(file_get_contents("php://input"));
$view_uuid = Common::sfilter($_REQUEST['view_uuid']);

if (!empty($input)) {
	$act = $input['act'];
	$view_uuid = Common::sfilter($input['view_uuid']);
}
//jssdk Ajax
if($act == 'jssdk'){
	require_once './source/include/cls_weixin_js.php';
	$cur_url = $_REQUEST['currentUrl'];

	$appid = $_lang['wx_config']['appid'];
	$appSecret = $_lang['wx_config']['appsecret'];
	$jssdk = new JSSDK($appid, $appSecret);
	$data = $jssdk->getSignPackage($cur_url);
	header('Content-Type: application/json;charset=utf-8');
	echo json_encode($data);
	exit;
}
//初始化全景项目
else if ($act == 'initPano') {
	//js 获取配置的json
	$pro = $Db->query("SELECT w.* , p.* FROM ".$Base->table('worksmain')." w LEFT JOIN ".$Base->table('pano_config')." p ON w.pk_works_main = p.pk_works_main WHERE w.view_uuid = '$view_uuid' AND flag_publish = 1","Row");
	if (empty($pro)) {
		die("未查询到相关项目");
	}
	$pro = Transaction::decode_str2arr($pro);
	$hotspots = &$pro['hotspot'];
	foreach ($hotspots as &$v) {
		$imgtext = &$v['imgtext'];
		if (!empty($imgtext)) {
			foreach ($imgtext as  &$v2) {
				if ($v2['imgtext_wordContent']) {
					$v2['imgtext_wordContent'] = base64_decode($v2['imgtext_wordContent']);
				}else if ($v2['wordContent']){
					$v2['imgtext_wordContent'] = base64_decode($v2['wordContent']);
					unset($v2['wordContent']);
				}
			}
		}
	}
	//底部菜单，只在手机端显示
	if(!Common::is_mobile_visit()){
		$pro['custom_menu'] = '';
	}
	//print_r($pro);exit;
	echo $Json->encode($pro);
	exit;
}
//点赞
else if($act == "add_praise"){
	if (!empty($view_uuid)) {
		$Db->execSql("UPDATE ".$Base->table('worksmain')." SET praised_num = praised_num+1 WHERE view_uuid = '$view_uuid'");
	}
	exit;
}
//校验密码
else if($act=="privacyAccess") {
	$re['status'] = 0;
	$pid = intval($_POST['pid']);
	$pwd =  Common::sfilter($_POST['pwd']);
	$pro = $Db->query('SELECT privacy_password , view_uuid FROM '.$Base->table('worksmain').' WHERE pk_works_main = '.$pid,'Row');

	if (empty($pro)||$pwd!=$pro['privacy_password']) {
		$re['msg'] ="密码有误";
	}else{
		$_SESSION['privacyAccess'][$pro['view_uuid']] = 1;
		$re['status'] = 1;
		$re['url'] = '/tour/'.$pro['view_uuid'];
	}
	echo $Json->encode($re);
	exit;
}
else{
	$pro = $Db->query("SELECT w.*,u.nickname FROM ".$Base->table('worksmain')." w LEFT JOIN ".$Base->table('user')." u ON u.pk_user_main = w.pk_user_main WHERE w.view_uuid = '$view_uuid' AND w.flag_publish = 1 AND u.state=0 ","Row");
	if (empty($pro)) {
		die("未查询到相关项目");
	}
	if(!empty($pro['privacy_password'])&&empty($_SESSION['privacyAccess'][$pro['view_uuid']])){
		//设置了访问密码并且没有登录
		$tp->assign("pid",$pro['pk_works_main']);
		$tp->display($_lang['moban']."/privacy.tpl");
		exit;
	}
	require_once ROOT_PATH.'plugin/plugin_init_function.php';
	plugin_get_plugins("view");
	$Db->execSql("UPDATE ".$Base->table('worksmain')." SET browsing_num = browsing_num+1 WHERE view_uuid = '$view_uuid'");
	//pro是否有tag为全景媒体
	$pro['is_tag'] = $Db->query("select id from ".$Base->table('tag_works')." where works_id=".$pro['pk_works_main']." and tag_id=62 limit 1","One");
	if($pro['is_tag']){ 
		$tp->assign('tag',62);
	}
	else{
		$tp->assign('module','pictures');
	}	
	$pro_nav_link = $pro['is_tag'] ? array('name'=>'全景媒体','link'=>'/pictures?tag=62') : array('name'=>'720摄影','link'=>'/pictures');
	$tp->assign('pro_nav_link',$pro_nav_link);
	
	$tp->assign('tour_top_ad',Transaction::get_ad_by_postion(2));
	$tp->assign('tour_bottom_ad',Transaction::get_ad_by_postion(3));
	// if($_REQUEST['debug']){
	// 	var_dump(Transaction::get_ad_by_postion(2));
	// 	var_dump(Transaction::get_ad_by_postion(3));
	// }

	//查询项目配置
	$worksmain = $Db->query("select p.definition,p.loading_img from ".$Base->table('pano_config')." as p left join ".$Base->table('worksmain')." as w on p.pk_works_main=w.pk_works_main where w.view_uuid='$view_uuid'");
	if(!Common::is_mobile_visit()){
		if(!$worksmain[0]['definition']){
			$tp->assign('tour_left_ad',Transaction::get_ad_by_postion(6));
		}
	}
	$loading_img = $Json->decode($worksmain[0]['loading_img']);
	$tp->assign('loading_img',$loading_img);
	// var_dump($loading_img);die;
	//footer信息
	$tp->assign('partner',Transaction::get_partner());
	$tp->assign('link',Transaction::get_link());
	$profile = preg_replace("/\s/","", Common::sfilter($pro['profile']));
	$pro['profile_share'] = mb_strlen($profile)>255 ? mb_substr($profile,0,255) : $profile;

	$share_title = preg_replace("/\s/","", Common::sfilter($pro['share_title']));
	$pro['share_title'] = mb_strlen($share_title)>255 ? mb_substr($share_title,0,255) : $share_title;

	$share_desc = preg_replace("/\s/","", Common::sfilter($pro['share_desc']));
	$pro['share_desc'] = mb_strlen($share_desc)>255 ? mb_substr($share_desc,0,255) : $share_desc;


	$tp->assign("pro",$pro);
	//查询系统标签的通知
    $sql = "SELECT t.notice,t.href FROM " . $Base->table('tag_works') . " as tw " .
           "left join" . $Base->table("tag") . " as t on t.id=tw.tag_id " .
           "WHERE tw.works_id = " . $pro['pk_works_main'] . " and t.notice !='' ".
		   "and t.time_s<='".date('Y-m-d',Common::gmtime())."' and t.time_e>='".date('Y-m-d',Common::gmtime())."' ".
		   "order by t.sort asc, t.id desc";
    $temp_data = $Db->query($sql);
    if(!empty($temp_data) && !empty($temp_data[0]["notice"])) {
        $tp->assign("notice", $temp_data[0]);
    } 
	else {
        //读取商户自己的
        $sql = "select notice,href from ".$Base->table("company_profile")." where pk_works_main=".$pro["pk_works_main"];
        $temp_data = $Db->query($sql);
        if (!empty($temp_data) && !empty($temp_data["notice"])) {
            $tp->assign("notice", array("notice" => $temp_data["notice"], "href" => $temp_data["href"]));
        }
    }
	if(!Common::is_mobile_visit() && $_REQUEST['v']=='pc'){
		//$tp->assign('is_pc',1);
		$tp->assign('title','六盘水'.$pro['name'].' |云上贵州-贵州六盘水VR全景图,VR全景视频,VR看凉都,VR看钟山,六盘水全景,中国凉都全景');
		$tp->assign('description',Common::sfilter($pro['profile']));
		$tp->display($_lang['moban'].'/tour_n.tpl');
		exit;
	}
	$tp->assign('view_uuid',$view_uuid);
	$tp->assign('startscene',Common::sfilter($_REQUEST['scene']));
	$tp->display($_lang['moban']."/tour.tpl");

}

?>